package com.starrk.myblog.foreground.config.shiro;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.starrk.myblog.foreground.entity.Authority;
import com.starrk.myblog.foreground.entity.Role;
import com.starrk.myblog.foreground.entity.TbBlogger;
import com.starrk.myblog.foreground.service.MPTbBloggerService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyShiroRealm extends AuthorizingRealm {
    @Autowired
    private MPTbBloggerService tbBloggerService;


    public MyShiroRealm(){
//        setName("UserRealm");
        // 采用MD5加密
//        setCredentialsMatcher(new HashedCredentialsMatcher("md5"));
    }

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = (String)principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // add Permission Resources
        //伪代码，之后改
        //权限获取
        List<Authority> authorities = tbBloggerService.findAuthorityByUsername(username);
        System.out.println("=========================================="+authorities);
        Set<String> permisssionSets = new HashSet<>();
        for (Authority perm:authorities) {
            permisssionSets.add(perm.getAuthorName());
        }
        info.setStringPermissions(permisssionSets);

        //角色获取
        List<Role> roles = tbBloggerService.findRolesByUsername(username);
        Set<String> rolenames = new HashSet<>();
        System.out.println("=========================================="+roles);
        for (Role role:roles) {
            rolenames.add(role.getRoleName());
        }
        info.addRoles(rolenames);


        // add Roles String[Set<String> roles]
        // info.setRoles(roles);
        return info;
    }

    /**
     * 认证
     * @param Atoken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken Atoken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken)Atoken;
        String username = token.getUsername();
        TbBlogger tbBlogger = tbBloggerService.selectOne(new EntityWrapper<TbBlogger>().where("username={0}",username));
        System.out.println("认证处调用的对象======"+tbBlogger);
        if (tbBlogger == null) {
            throw new UnknownAccountException();
        }
        String password =tbBlogger.getPassword();

//      ByteSource.Util.bytes(tbBlogger.getSalt())
        SimpleAuthenticationInfo AInfo = new SimpleAuthenticationInfo(username,password, ByteSource.Util.bytes(tbBlogger.getSalt()),getName());
        return AInfo;
    }
}
